package com.question_barrier.service.impl;

import com.question_barrier.entity.AnswerState;
import com.question_barrier.repository.AnswerStateRepository;
import com.question_barrier.result.StateCode;
import com.question_barrier.service.AnswerStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AnswerStateServiceImpl implements AnswerStateService {
    @Autowired
    AnswerStateRepository answerStateRepository;

    @Override
    public AnswerState findByOpenId(String openid){
        return answerStateRepository.findByOpenId(openid);
    }

    @Override
    public boolean existsByOpenId(String openId){
        return answerStateRepository.existsByOpenId(openId);
    }

    @Override
    public boolean existsByOpenIdAndQuestionId(String openId, String questionId){
        return answerStateRepository.existsByOpenIdAndQuestionId(openId, questionId);
    }
    @Override
    public int insAnswerState(AnswerState answerState){
        AnswerState answerState1Save = answerStateRepository.save(answerState);
        if(answerState1Save != null){
            return StateCode.ADDSUCCESS;
        }
        return StateCode.ADDFAIL;
    }
    @Override
    public AnswerState findByOpenIdAndQuestionId(String openId, String questionId){
        return answerStateRepository.findByOpenIdAndQuestionId(openId, questionId);
    }
    @Override
    public int updAnswerState(AnswerState answerState){
        AnswerState answerState1Save = answerStateRepository.save(answerState);
        if(answerState1Save != null){
            return StateCode.UPDATESUCCESS;
        }
        return StateCode.UPDATEFAIL;
    }
}
